package org.pearl.thread.demo.test001;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by TD on 2021/4/12
 */
public class ExecutorsTest002 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        try {
            // 创建10个线程并执行
            for (int i = 0; i < 10; i++) {
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName() + "线程正在运行任务");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭线程池
            executorService.shutdown();
        }
    }
}
